/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package shoppingservice.view;

import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;
import javafx.scene.input.MouseEvent;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.CustomNode;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.image.Image;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import shoppingservice.model.ProductCatalog;
import shoppingservice.Main;
import shoppingservice.Utils;

/**
 * View used to display product details
 */

public def height = 247.0;
public def width = 238.0;

public def waitImage = Image { url: "{__DIR__}images/wait.png" };

public class ProductInfoView extends CustomNode { 
        
    override var translateX = 0;
    override var translateY = 38;
    var loadingData = false;
    
    public var show = false on replace {
        fader();
    }
    
    var thumbImage: ThumbImageView = ThumbImageView { };
    
    var productNameDivider = Line {
        startX: 8  startY: 135
        endX: width - 8 endY: 135
        stroke: Color.rgb(185, 185, 185)
    };

    var productNameLabel: Text = Text {
        x: 30
        y: 150
        font: Font { size: 12 }
        content: "이름:"
    };
    
    var productName: Text = Text {
        x: 80
        y: 150
        font: Font { size: 12 }
        fill: Color.BLACK

        content: ""
    };

    var companyDivider = Line {
        startX: 8  startY: 155
        endX: width - 8 endY: 155
        stroke: Color.rgb(185, 185, 185)
    };
    
    var companyLabel = Text {
        x: 30
        y: 170
        font: Font { name: "Bitstream Vera Sans" size: 12 }
        content: "제조:"
    };
    
    var company = Text {
        x: 80
        y: 170
        font: Font { name: "Bitstream Vera Sans" size: 12 }
        content: ""
    };

    var priceDivider = Line {
        startX: 8  startY: 175
        endX: width - 8 endY: 175
        stroke: Color.rgb(185, 185, 185)
    };
    
    var priceLabel = Text {
        x: 30
        y: 190
        font: Font { name: "Bitstream Vera Sans Bold" size: 12 }
        content: "가격:"
    };
    
    var price = Text {
        x: 80
        y: 190
        font: Font { name: "Bitstream Vera Sans Bold" size: 12 }
        fill: Color.rgb(123, 197, 70)
        content: ""
    };

    var qtyDivider = Line {
        startX: 8  startY: 195
        endX: width - 8 endY: 195
        stroke: Color.rgb(185, 185, 185)
    };
    
    var qtyLabel = Text {
        x: 30
        y: 210
        font: Font { name: "Bitstream Vera Sans" size: 12 }
        content: "수량:"
    };
    
    var qty = Text {
        x: 80
        y: 210
        font: Font { name: "Bitstream Vera Sans" size: 12 }
        content: ""
    };

    var categoryDivider = Line {
        startX: 8  startY: 215
        endX: width - 8 endY: 215
        stroke: Color.rgb(185, 185, 185)
    };
    
    var categoryLabel = Text {
        x: 30
        y: 230
        font: Font { name: "Bitstream Vera Sans" size: 12 }
        content: "분류:"
    };
    
    var category = Text {
        x: 80
        y: 230
        font: Font { name: "Bitstream Vera Sans" size: 12 }
        content: ""
    };
    
    var bgRect = Rectangle {
        x: 0 y: 0
        width: width height: height
        fill: Color.TRANSPARENT
    }
    
    var detailsRect = Rectangle {
        x: 8 y: 5
        width: width - 14
        height: height - 11
        fill: Color.WHITE
        arcHeight: 20
        arcWidth: 20
    }

    override function create():Node {
        
        visible = false;
        blocksMouse = true;
        
        Group { 
            content: [ 
                bgRect, detailsRect, 
                Group {
                    content: [
                        productNameDivider, productNameLabel, productName,
                        companyDivider, companyLabel, company,
                        priceDivider, priceLabel, price,
                        qtyDivider, qtyLabel, qty,
                        categoryDivider, categoryLabel, category
                    ]
                    opacity: bind (1.0 - (thumbImage.borderOpacity/1.5))
                }, 
                thumbImage
            ] 
        };
    }

    override var onMousePressed = function(e:MouseEvent) {
        show = false;
    }

    function fader() {
        
        // Ignore Fader for Mobile
        if("{__PROFILE__}" == "mobile") { 
            visible = show;
            return; 
        }
        
        var endOpacity = 0.0;
        if(show) {
            endOpacity = 1.0;
            visible = true;
        } else {
            endOpacity = 0.0;
        }
                        
        /**
         * Make details-view appear slowly by changing opacity
         */
        var viewFaderTimeline:Timeline = Timeline {
            keyFrames: [ 
                KeyFrame {
                    canSkip: true
                    time: 500ms
                    values: [ opacity => endOpacity tween Interpolator.LINEAR ] 
                    action: function() {
                        visible = show;
                    }
                }
            ]
        };
        viewFaderTimeline.play();
    }
    
    /**
     * Make text in details-view appear slowly by changing opacity
     */
    var textFaderTimeline:Timeline = Timeline {
        keyFrames: [ 
            KeyFrame {
                canSkip: true
                time: 1s
                values: [ thumbImage.borderOpacity => 0.0 tween Interpolator.LINEAR ] 
                action: function() {
                    loadingData = false;
                }
            }
        ]
    };
    
    function textFader() {
        if("{__PROFILE__}" == "mobile") { return; } // Ignore Fader for Mobile
        loadingData = true;
        thumbImage.borderVisible = false;
        thumbImage.borderOpacity = 1.0;
        textFaderTimeline.playFromStart();
    }
    
    public var product: ProductCatalog on replace {
        
        if(product != null) {
            
            // Reset
            show = true;
            textFader();
            thumbImage.borderVisible = false;
            
            productName.content = Utils.trimString(product.name, 30);
            company.content = Utils.trimString(product.company, 30);
            price.content = "{Utils.trimString(product.price, 12)} 원";
            qty.content = "{Utils.trimString(product.productqty, 5)} 개";
            category.content = Utils.trimString(product.category, 30);
            
            thumbImage.image = Image { 
                url: "{__DIR__}images/{product.image.toString()}.png";
                backgroundLoading: true
                placeholder: waitImage
            };
        }
    }
    
    public function reorderNodes() {
        if(product == null) {
            if((sizeof Main.products) == 0) { return; }
            product = Main.products[0];
        }
        show = true;
    }
}